home *** CD-ROM | disk | FTP | other *** search
- // pluginTest.cpp
- //
- // This file makes a test plugin -- a plugin intended to demonstrate and test
- // as many parts of the REALbasic plugin SDK as possible. It is based on the
- // boxControl plug-in, so has many initial similarities; but we'll keep
- // boxControl simple while this one will grow more and more complex as we add
- // more test cases. Feel free to send me more test code for this plugin!
- //
- // Document Author: Joe Strout <joe@realsoftware.com>
- //
- // Revision History
- // Nov 07 2000 -- JJS (1) initial creation, adapted from boxControl.cpp
- // Jan 24 2001 -- JJS (1) added test of static/global class constructors/destructors
-
- // include the RB plugin headers
- #include "rb_plugin.h"
-
- // test of static/global class constructors/destructors
- static class testclass
- {
- public:
- testclass();
- ~testclass();
- } testclassobj;
-
- testclass::testclass()
- {
- // To test global constructors, uncomment this line:
- //DebugStr("\pconstructing testclassobj");
- }
-
- testclass::~testclass()
- {
- // To test global destructors, uncomment this line:
- //DebugStr("\pdestructing testclassobj");
- }
-
-
- // forward declaration of our custom controls and classes
- extern struct REALcontrol testControl;
-
- REALevent testEvents[] = {
- { "Action(Red As Integer)" }
- };
-
- struct testData
- {
- int fillColor; // color in RB color format (xxRRGGBB)
- };
-
- static void testInit(REALcontrolInstance)
- {
- }
-
- #pragma mark testDraw
- #ifdef TARGET_WIN32
- static void testDraw(REALcontrolInstance instance, REALgraphics context)
- #else
- static void testDraw(REALcontrolInstance instance)
- #endif
- {
- // Find the bounds rect
- Rect r;
- REALGetControlBounds(instance, &r);
-
- // Make sure our aspect ratio is constant.
- short height = r.bottom-r.top, width = r.right-r.left;
- if (width < height) {
- // shorten height to match width
- height = width;
- r.bottom = r.top + height;
- if (REALinRuntime()) REALSetControlPosition( instance, kREALHeight, height );
- } else if (height < width) {
- // shorten width to match height
- width = height;
- r.right = r.left + width;
- if (REALinRuntime()) REALSetControlPosition( instance, kREALWidth, width );
- }
-
- // Get our custom data into "data"
- ControlData(testControl, instance, testData, data);
-
- // Do the drawing (using native graphics tooltest)
- #if TARGET_WIN32
- // Windows implementation:
-
- HDC hDC = REALGraphicsDC(REALGetControlGraphics(instance));
-
- HBRUSH fillBrush = CreateSolidBrush(RGB(
- (data->fillColor >> 16), (data->fillColor >> 8) & 0xFF, (data->fillColor & 0xFF)));
- HBRUSH lineBrush = CreateSolidBrush(RGB(0,0,0));
- HPEN oldPen, linePen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
-
- RECT winr;
- ::SetRect(&winr, r.left, r.top, r.right, r.bottom);
- ::FillRect(hDC, &winr, fillBrush);
-
- ::FrameRect(hDC, &winr, lineBrush);
-
- oldPen = (HPEN) SelectObject(hDC, linePen);
- ::MoveToEx(hDC, r.left, r.top, nil);
- ::LineTo(hDC, r.right - 1, r.bottom - 1);
-
- SelectObject(hDC, oldPen);
-
- DeleteObject(fillBrush);
- DeleteObject(lineBrush);
- DeleteObject(linePen);
-
- #else
- // Mac implementation:
- RGBColor oldCol, fillCol, lineCol = {0,0,0};
-
- GetForeColor(&oldCol);
- fillCol.red = (data->fillColor >> 16) * 257;
- fillCol.green = ((data->fillColor >> 8) & 0xff) * 257;
- fillCol.blue = ((data->fillColor & 0xff) * 257);
- RGBForeColor(&fillCol);
- PaintRect(&r);
-
- RGBForeColor(&lineCol);
- FrameRect(&r);
-
- MoveTo(r.left, r.top);
- LineTo(r.right - 1, r.bottom - 1);
-
- RGBForeColor(&oldCol);
- #endif
- }
-
- static void testMakeRed(REALcontrolInstance instance)
- {
- // get our private data
- ControlData(testControl, instance, testData, data);
-
- // make the test red
- data->fillColor = 0xFF0000;
-
- // invoke the event handler, if there is one
- void (*fp)(REALcontrolInstance instance, int);
- fp = (void (*)(REALcontrolInstance instance, int)) REALGetEventInstance(instance, &testEvents[0]);
- if (fp) fp(instance, data->fillColor >> 16);
-
- // just for testing, let's also try out some of the newer control methods
- REALSetControlPosition( instance, kREALTop, REALGetControlPosition(instance, kREALTop) - 20 );
- }
-
- static REALmemoryBlock TestNewMemoryBlock(REALcontrolInstance, int bytes)
- {
-
- // Test the REALNewMemoryBlock function by simply calling it and returning
- // the result to the RB app, which can check its validity.
- return REALNewMemoryBlock(bytes);
- }
-
- static int TestMemoryBlock(REALcontrolInstance)
- {
- // This is a general test function for all the MemoryBlock APIs.
- // Returns 0 for no error, nonzero for errors.
- REALmemoryBlock m = REALNewMemoryBlock(42);
- if (not m) return 1;
- if (REALMemoryBlockGetSize(m) != 42) return 2;
- REALUnlockObject((REALobject)m);
-
- if (REALNewMemoryBlock(-5)) return 3;
- if (REALNewMemoryBlock(0)) return 4;
-
- char buf[] = "01234567";
- m = REALPtrToMemoryBlock(buf);
- if (not m) return 5;
- unsigned long *ptr = (unsigned long*)REALMemoryBlockGetPtr(m);
- if (not ptr) return 6;
- if (ptr[0] != '0123' or ptr[1] != '4567') return 7;
- REALUnlockObject((REALobject)m);
-
- return 0;
- }
-
- static REALpicture TestNewPicture(REALcontrolInstance)
- {
- // Test picture creation and manipulation.
- REALpicture p = REALNewPicture(64,64,32);
- if (not p) return p;
-
- REALgraphics g = REALGetPictureGraphics(p);
- if (g) {
- #if WIN32
- // use REALGraphicsDC followed by Win32 drawing commands...
- #else
- REALSelectGraphics(g);
- RGBColor red = {0xFFFF,0,0}, green = {0,0xFFFF,0};
- RGBForeColor(&red);
- Rect r = {0,0,32,32};
- PaintRect(&r);
- OffsetRect(&r, 32,32);
- RGBForeColor(&green);
- PaintRect(&r);
- #endif
- }
-
- return p;
- }
-
- static int TestStringToOSType(REALcontrolInstance, REALstring s)
- {
- return REALstringToOSType(s);
- }
-
- static int testColorComponent(REALcontrolInstance instance, int bit)
- {
- ControlData(testControl, instance, testData, data);
-
- if (bit == 0)
- return data->fillColor >> 16;
- else if (bit == 1)
- return (data->fillColor >> 8) & 0xff;
- else
- return (data->fillColor) & 0xff;
- }
-
- REALproperty testProperties[] = {
- { "Appearance", "BackColor", "Color", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(testData, fillColor) }
- };
-
- REALmethodDefinition testMethods[] = {
- { (REALproc) testMakeRed, REALnoImplementation, "MakeRed" },
- { (REALproc) testColorComponent, REALnoImplementation, "ColorComponent(v as integer) as integer" },
- { (REALproc) TestNewMemoryBlock, REALnoImplementation, "NewMemoryBlock(size as Integer) as MemoryBlock" },
- { (REALproc) TestMemoryBlock, REALnoImplementation, "TestMemoryBlock as Integer" },
- { (REALproc) TestNewPicture, REALnoImplementation, "TestNewPicture as Picture" },
- { (REALproc) TestStringToOSType, REALnoImplementation, "TestStringToOSType(s as String) as Integer" },
- };
-
- REALcontrolBehaviour testBehaviour = {
- testInit,
- nil,
- testDraw,
- nil,
- nil,
- nil
- };
-
- static void globTest(int *foo)
- {
- for (int i=0; i<*foo; i++) {
- #if !WIN32
- SysBeep(3);
- #endif
- }
- *foo = 42;
- }
-
- REALcontrol testControl = {
- kCurrentREALControlVersion,
- "PluginTest",
- sizeof(testData),
- REALcontrolAcceptFocus | REALcontrolFocusRing,
- 128,
- 129,
- 32, 32,
- testProperties,
- sizeof(testProperties) / sizeof(REALproperty),
- testMethods,
- sizeof(testMethods) / sizeof(REALmethodDefinition),
- testEvents,
- sizeof(testEvents) / sizeof(REALevent),
- &testBehaviour
- };
-
- static int testGetControlHandle(REALcontrolInstance instance)
- {
- return (int)REALGetControlHandle(instance);
- }
-
- REALmethodDefinition testGetControlHandleDef =
- { (REALproc) testGetControlHandle, REALnoImplementation, "TestGetControlHandle(c as Control) as Integer" };
-
-
- void PluginEntry(void)
- {
- #if CARBON
- DebugStr("\pRegistering control");
- #endif
- REALRegisterControl(&testControl);
-
- REALmethodDefinition globTestMethod = { (REALproc) globTest, REALnoImplementation,
- "PluginByRefTest(ByRef foo as Integer)" };
- REALRegisterMethod(&globTestMethod);
- REALRegisterMethod(&testGetControlHandleDef);
- }
-